package com.senior.sendmessage.util;

import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;


/**
 * redis服务
 *
 * @author bjl_m
 * @date 2021/11/11
 */
public class RedisService {
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void setString(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public String getString(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public void setList(String key, List list) {
        redisTemplate.opsForValue().set(key, JSONUtil.toJsonPrettyStr(list));
    }

    public List getList(String key) {
        String json = redisTemplate.opsForValue().get(key);
        return JSONUtil.toBean(json, List.class);
    }

    public void del(String key) {
        redisTemplate.delete(key);
    }
}
